package samoloty.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.Point;

import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.text.StyledEditorKit.BoldAction;

/**
 *
 * @author borysfan
 */
public class MainWindow extends JFrame {

    private JPanel mainPanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private ContentPanel panel;
    private MyLabel titleBar;
    private MyLabel topLeftCorner;
    private MyLabel flightTitle;
    //BUTTONY - LEWY EKRAN
    private MyNewButton flightBtn;
    private MyNewButton dowBtn;
    private MyNewButton fuelBtn;
    private MyNewButton paxBtn;
    private MyNewButton holdsBtn;
    private MyNewButton siBtn;
    //BUTTONY - PRAWY EKRAN
    private MyNewButton displayBtn;
    private MyNewButton printBtn;
    private MyNewButton saveBtn;
    private MyNewButton helpBtn;
    private MyNewButton resetBtn;
    private MyNewButton exitBtn;

    public MainWindow() {
        super();
        initFrame();

    }

    private void initFrame() {
        this.initButtons();


        //wysrodkowanie wyswietlanego programu
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int width = 768;
        int height = 576;
        this.setBounds((screen.width - width) / 2, (screen.height - height) / 2, width, height);

        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setBackground(new Color(0, 0, 0));
        this.setResizable(false);
        this.setUndecorated(true);
        this.setName("Airport");
        topLeftCorner = new MyLabel("AHH 550", MyLabel.BOLD);
        topLeftCorner.setPosition(new Point(3, 0));
        flightTitle = new MyLabel("                                                                         ", MyLabel.BOLD);
        flightTitle.setPosition(new Point(354, 0));

        titleBar = new MyLabel("                                                                            ", MyLabel.BOLD);
        titleBar.setSize(200, titleBar.getHeight());
        titleBar.setAlignmentX(MyLabel.CENTER_ALIGNMENT);
        titleBar.setPosition(new Point(250, 0));

        mainPanel = new JPanel();
        mainPanel.setOpaque(true);
        mainPanel.setBounds(0, 0, 768, 576);
        mainPanel.setBackground(MyColors.getBlack());
        //mainPanel.setBorder(BorderFactory.createLineBorder(MyColors.getBlue(), 1));
        mainPanel.setLayout(null);
        mainPanel.add(topLeftCorner);
        mainPanel.add(titleBar);
        this.initLeftPanel();
        this.initRightPanel();
        panel = new MainPanel();


        mainPanel.add(flightTitle);

        mainPanel.add(leftPanel);
        mainPanel.add(panel);
        mainPanel.add(rightPanel);

        this.add(mainPanel);
        //setFocusableComponents(false);

    }

    public void setFocusableComponents(boolean b) {
        leftPanel.setFocusable(b);
        mainPanel.setFocusable(b);
        rightPanel.setFocusable(b);
    }

    public void setFlightTitle(String title) {
        flightTitle.setText(title);
    }

    public void toggleFlightTitle(boolean visibility) {
        flightTitle.setVisible(visibility);
    }

    public JPanel getLeftPanel() {
        return leftPanel;
    }

    public JPanel getRightPanel() {
        return rightPanel;
    }

    public JPanel getCenterPanel() {
        return mainPanel;
    }

    private void initLeftPanel() {
        leftPanel = new JPanel();
        leftPanel.setOpaque(true);
        leftPanel.setBorder(null);
        leftPanel.setBounds(0, 1, 94, 574);
        leftPanel.setPreferredSize(new Dimension(94, 574));
        leftPanel.setBackground(MyColors.getBlack());

        leftPanel.setLayout(null);
        leftPanel.add(flightBtn);
        leftPanel.add(dowBtn);
        leftPanel.add(fuelBtn);
        leftPanel.add(paxBtn);
        leftPanel.add(holdsBtn);
        leftPanel.add(siBtn);

        flightBtn.setPosition(new Point(2, 50));
        dowBtn.setPosition(new Point(2, 145));
        fuelBtn.setPosition(new Point(2, 240));
        paxBtn.setPosition(new Point(2, 335));
        holdsBtn.setPosition(new Point(2, 430));
        siBtn.setPosition(new Point(2, 525));
    }

    private void initRightPanel() {
        rightPanel = new JPanel();
        rightPanel.setOpaque(true);
        rightPanel.setBorder(null);
        rightPanel.setBounds(672, 1, 94, 574);
        rightPanel.setPreferredSize(new Dimension(94, 574));
        rightPanel.setBackground(MyColors.getBlack());
        rightPanel.setLayout(null);

        rightPanel.add(displayBtn);
        rightPanel.add(printBtn);
        rightPanel.add(saveBtn);
        rightPanel.add(helpBtn);
        rightPanel.add(resetBtn);
        rightPanel.add(exitBtn);

        displayBtn.setPosition(new Point(2, 50));
        printBtn.setPosition(new Point(2, 145));
        saveBtn.setPosition(new Point(2, 240));
        helpBtn.setPosition(new Point(2, 335));
        resetBtn.setPosition(new Point(2, 430));
        exitBtn.setPosition(new Point(2, 525));
        exitBtn.setFocusable(true);
    }

    private void initButtons() {
        flightBtn = new MyNewButton("FLIGHT");
        dowBtn = new MyNewButton("DOW");
        fuelBtn = new MyNewButton("FUEL");
        paxBtn = new MyNewButton("PAX");
        holdsBtn = new MyNewButton("HOLDS");
        siBtn = new MyNewButton("SI");

        displayBtn = new MyNewButton("DISPLAY", "       L/S");
        printBtn = new MyNewButton("PRINT", "     L/S");
        saveBtn = new MyNewButton("SAVE");

        helpBtn = new MyNewButton("HELP");
        resetBtn = new MyNewButton("RESET");
        exitBtn = new MyNewButton("EXIT");
    }

    /*LEFT PANEL BUTTONS*/
    public void setLeftPanelVisible(boolean value) {
        setFlightVisible(value);
        setDowVisible(value);
        setFuelVisible(value);
        setPaxVisible(value);
        setHoldsVisible(value);
        setSiVisible(value);
    }

    public void setTitleBar(String s) {
        titleBar.setText(s);
    }

    public String getTitleBar() {
        return titleBar.getText();
    }

    public void setFlightVisible(boolean value) {
        this.flightBtn.setVisible(value);
    }

    public void setDowVisible(boolean value) {
        this.dowBtn.setVisible(value);
    }

    public void setFuelVisible(boolean value) {
        this.fuelBtn.setVisible(value);
    }

    public void setPaxVisible(boolean value) {
        this.paxBtn.setVisible(value);
    }

    public void setHoldsVisible(boolean value) {
        this.holdsBtn.setVisible(value);
    }

    public void setSiVisible(boolean value) {
        this.siBtn.setVisible(value);
    }

    /*RIGHT PANEL BUTTONS*/
    public void setRightPanelVisible(boolean value) {
        setDisplayVisible(value);
        setPrintVisible(value);
        setSaveVisible(value);
        setHelpVisible(value);
        setResetVisible(value);
        setExitVisible(value);
    }

    public void setDisplayVisible(boolean value) {
        this.displayBtn.setVisible(value);
    }

    public void setPrintVisible(boolean value) {
        this.printBtn.setVisible(value);
    }

    public void setSaveVisible(boolean value) {
        this.saveBtn.setVisible(value);
    }

    public void setHelpVisible(boolean value) {
        this.helpBtn.setVisible(value);
    }

    public void setResetVisible(boolean value) {
        this.resetBtn.setVisible(value);
    }

    public void setExitVisible(boolean value) {
        this.exitBtn.setVisible(value);
    }

    /*Zmienianie glownego panelu*/
    public void setPanel(ContentPanel p) {
        int index = findComponentIndex();
        if (index >= 0) {
            mainPanel.remove(panel);
            mainPanel.add(p, index);
            panel = p;
            this.remove(mainPanel);
            setContentPane(mainPanel);

        }
    }

    public void visibleLeftCorenerTitle() {
        topLeftCorner.setVisible(true);
    }

    public void unvisibleLeftCorenerTitle() {
        topLeftCorner.setVisible(false);
    }

    /*ActionListeners set functions*/
    public void setFlightBtnListener(ActionListener listener) {
        flightBtn.addActionListener(listener);
    }

    public void setDowBtnListener(ActionListener listener) {
        dowBtn.addActionListener(listener);
    }

    public void setFuelBtnListener(ActionListener listener) {
        fuelBtn.addActionListener(listener);
    }

    public void setPaxBtnListener(ActionListener listener) {
        paxBtn.addActionListener(listener);
    }

    public void setHoldBtnListener(ActionListener listener) {
        holdsBtn.addActionListener(listener);
    }

    public void setDisplayBtnListener(ActionListener listener) {
        displayBtn.addActionListener(listener);
    }

    public void setHelpBtnListner(ActionListener listener) {
        helpBtn.addActionListener(listener);
    }

    public void setResetBtnListener(ActionListener listener) {
        resetBtn.addActionListener(listener);
    }

    public void setExitBtnListener(ActionListener listner) {
        exitBtn.addActionListener(listner);
    }

    public void setSiBtnListener(ActionListener listener) {
        siBtn.addActionListener(listener);
    }

    public void setSaveBtnListener(ActionListener listener) {
        saveBtn.addActionListener(listener);
    }

    public MyButton getExitBtn() {
        return exitBtn;
    }

    /*WYGLAD PRZYCISKOW*/
    public void setFlightBtnStyle(int type) {
        if (type == MyButton.ACTIVE_BUTTON) {
            flightBtn.setAsActive();
        } else if (type == MyButton.UNACTIVE_BUTTON) {
            flightBtn.setAsUnactive();
        } else {
            flightBtn.setAsChoose();
        }

    }

    public void setDowBtnStyle(int type) {
        if (type == MyButton.ACTIVE_BUTTON) {
            dowBtn.setAsActive();
        } else if (type == MyButton.UNACTIVE_BUTTON) {
            dowBtn.setAsUnactive();
        } else {
            dowBtn.setAsChoose();
        }
    }

    public void setFuelBtnStyle(int type) {
        if (type == MyButton.ACTIVE_BUTTON) {
            fuelBtn.setAsActive();
        } else if (type == MyButton.UNACTIVE_BUTTON) {
            fuelBtn.setAsUnactive();
        } else {
            fuelBtn.setAsChoose();
        }
    }

    public void setPaxBtnStyle(int type) {
        if (type == MyButton.ACTIVE_BUTTON) {
            paxBtn.setAsActive();
        } else if (type == MyButton.UNACTIVE_BUTTON) {
            paxBtn.setAsUnactive();
        } else {
            paxBtn.setAsChoose();
        }
    }

    public void setHoldsBtnStyle(int type) {
        if (type == MyButton.ACTIVE_BUTTON) {
            holdsBtn.setAsActive();
        } else if (type == MyButton.UNACTIVE_BUTTON) {
            holdsBtn.setAsUnactive();
        } else {
            holdsBtn.setAsChoose();
        }
    }

    public void setSiBtnStyle(int type) {
        if (type == MyButton.ACTIVE_BUTTON) {
            siBtn.setAsActive();
        } else if (type == MyButton.UNACTIVE_BUTTON) {
            siBtn.setAsUnactive();
        } else {
            siBtn.setAsChoose();
        }
    }

    public void setDisplayBtnStyle(int type) {
        if (type == MyButton.ACTIVE_BUTTON) {
            displayBtn.setAsActive();
        } else if (type == MyButton.UNACTIVE_BUTTON) {
            displayBtn.setAsUnactive();
        } else {
            displayBtn.setAsChoose();
        }
    }

    public void setPrintBtnStyle(int type) {
        if (type == MyButton.ACTIVE_BUTTON) {
            printBtn.setAsActive();
        } else if (type == MyButton.UNACTIVE_BUTTON) {
            printBtn.setAsUnactive();
        } else {
            printBtn.setAsChoose();
        }
    }

    public void setSaveBtnStyle(int type) {
        if (type == MyButton.ACTIVE_BUTTON) {
            saveBtn.setAsActive();
        } else if (type == MyButton.UNACTIVE_BUTTON) {
            saveBtn.setAsUnactive();
        } else {
            saveBtn.setAsChoose();
        }
    }

    public void setHelpBtnStyle(int type) {
        if (type == MyButton.ACTIVE_BUTTON) {
            helpBtn.setAsActive();
        } else if (type == MyButton.UNACTIVE_BUTTON) {
            helpBtn.setAsUnactive();
        } else {
            helpBtn.setAsChoose();
        }
    }

    public void setAllBtnStyle(int type) {
        setFlightBtnStyle(type);
        setDowBtnStyle(type);
        setFuelBtnStyle(type);
        setPaxBtnStyle(type);
        setHoldsBtnStyle(type);
        setSiBtnStyle(type);
        setDisplayBtnStyle(type);
        setPrintBtnStyle(type);
        setHelpBtnStyle(type);
    }

    private int findComponentIndex() {
        Component[] components = mainPanel.getComponents();
        for (int i = 0; i < components.length; i++) {
            if (components[i] instanceof ContentPanel) {
                return i;
            }
        }
        return -1;
    }

    public void setPrintBtnListener(ActionListener printListener) {
        printBtn.addActionListener(printListener);
    }

    public void setKeyListener(KeyListener listener) {
        this.addKeyListener(listener);
    }

    public void setButtonsInActive() {
        flightBtn.setFocusable(false);
        dowBtn.setFocusable(false);
        fuelBtn.setFocusable(false);
        paxBtn.setFocusable(false);
        holdsBtn.setFocusable(false);
        siBtn.setFocusable(false);
        displayBtn.setFocusable(false);
        printBtn.setFocusable(false);
        saveBtn.setFocusable(false);
        helpBtn.setFocusable(false);
        resetBtn.setFocusable(false);
        exitBtn.setFocusable(false);
    }

    public void setButtonsActive() {
        helpBtn.setFocusable(true);
        resetBtn.setFocusable(true);
        exitBtn.setFocusable(true);
    }
}
